package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ErrorReadCapEntity
import com.hzjq.core.bean.ErrorResult
import com.hzjq.core.callback.Callback
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work
import io.reactivex.functions.Consumer

class BlastWork : Work<CapProgressEntity> {

    private var errorCallback: Consumer<ErrorReadCapEntity>?=null
    private var mBlastErrorWork:BlastErrorWork?=null

    constructor(callback: Callback<CapProgressEntity>?, errorCallback: Consumer<ErrorReadCapEntity>) : super(callback){
        this.errorCallback = errorCallback
    }

    override fun doWork(vararg args: Any) {
        Receives.getInstance()
            .registerReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader().getBlastCmd(),
                object : Receiver {
                    override fun convert(msg: String): Any {
                        return msg
                    }

                    override fun onSuccess(msg: Any) {
                        BlastDelegate.getDelegate().postDelay(BlastDelegate.getDelegate().getQueryDelayTime(), Runnable {
                            doNext(true,System.currentTimeMillis())
                        })
                    }

                    override fun failed() {
                        BlastDelegate.getDelegate().postDelay(BlastDelegate.getDelegate().getQueryDelayTime(), Runnable {
                            onProgressChanged(50, "爆破失败")
                            mBlastErrorWork = BlastErrorWork(object : ProgressCallback<ErrorReadCapEntity>{
                                override fun onResult(t: ErrorReadCapEntity) {
                                    errorCallback?.accept(t)
                                }

                                override fun onRetryCountChanged(retryCount: Int, action: String) {
                                    if(callback is ProgressCallback){
                                        (callback as ProgressCallback<Boolean>).onRetryCountChanged(retryCount, action)
                                    }
                                }

                                override fun onError(errorCode: ErrorResult) {
                                    callback?.onError(errorCode)
                                }

                                override fun onProgressChanged(
                                    progress: Int,
                                    total: Int,
                                    action: String
                                ) {
                                   if(callback is ProgressCallback){
                                       (callback as ProgressCallback<Boolean>).onProgressChanged(progress, total, action)
                                   }
                                }

                            })
                            mBlastErrorWork?.doWork()
                        })
                    }

                })

        val msg = DataMessageBean(BlastDelegate.getDelegate().getAssemblyCmdLoader().getBlastCmd().cmd)
        BlastDelegate.getDelegate().getCmdExeLoader().exePollResultCmd(msg.assembly(),callback)
    }

    override fun cancel() {
        mBlastErrorWork?.cancel()
        Receives.getInstance()
            .unRegisterReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader().getBlastCmd())
    }
}